from django.utils.deprecation import MiddlewareMixin
from users.models import UserModel
from users.utils import MyJwt
from rest_framework.response import Response

class Check_login(MiddlewareMixin):

    def process_request(self,request):
        token = request.headers.data('token')
        if not token:
            return Response({'errcode': 1, 'msg': 'token不存在'})
        myjwt = MyJwt()
        try:
            user = myjwt.decode(token)
        except Exception as e:
            print(str(e))
            return Response({'errcode': 2, 'msg': 'token不合法'})
        try:
            user = UserModel.objects.filter(id=user["id"])
            self._cuser = user
        except Exception as e:
            return Response({'errcode': 3, 'msg': '该用户不存在'})


